"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.encryptUTF8As = exports.encryptAs = exports.encryptUTF8 = exports.encrypt = exports.createKey = exports.checkKey = void 0;
const utils_1 = require("../utils");
const functions_1 = require("../decrypt/functions");
const createKey = () => arrayToString(randArray(utils_1.configKey.getKey()));
exports.createKey = createKey;
const checkKey = (yourKey) => JSON.stringify(yourKey.split("").sort()) ===
    JSON.stringify(utils_1.configKey.getKey().sort())
    ? true
    : false;
exports.checkKey = checkKey;
const filpedKey = functions_1.filp(utils_1.configKey.getKey());
function arrayToString(arr) {
    let str = "";
    arr.forEach(v => (str = str.concat(v)));
    return str;
}
function randArray(arr) {
    let result = [];
    arr.forEach((v, k) => (result[k] = v));
    return result.sort(() => (Math.random() > 0.5 ? -1 : 1));
}
function encrypt(str) {
    let cipher = "";
    const encryptKey = randArray(utils_1.configKey.getKey());
    const solution = arrayToString(encryptKey);
    str.split("").forEach((_v, k) => {
        const index = (Number(filpedKey[str[k]]) + k) % utils_1.configKey.getKey().length;
        cipher = cipher.concat(encryptKey[index]);
    });
    return {
        cipher,
        solution
    };
}
exports.encrypt = encrypt;
function encryptAs(str, solution) {
    let cipher = "";
    const encryptKey = functions_1.strToArray(solution);
    str.split("").forEach((_v, k) => {
        const index = (Number(filpedKey[str[k]]) + k) % utils_1.configKey.getKey().length;
        cipher = cipher.concat(encryptKey[index]);
    });
    return {
        cipher,
        solution
    };
}
exports.encryptAs = encryptAs;
function encryptUTF8(str) {
    return encrypt(encodeURI(str));
}
exports.encryptUTF8 = encryptUTF8;
function encryptUTF8As(str, solution) {
    return encryptAs(encodeURI(str), solution);
}
exports.encryptUTF8As = encryptUTF8As;
